package cn.sfinger.huifu.p2p.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.sfinger.huifu.p2p.base.HuifuConstant;
import cn.sfinger.huifu.p2p.integration.HuifuIntegration;

/**
 * 汇付后台通知入口
 * 
 * @author michael.wang.54@gmail.com
 * @version 1.0.0
 * @date 2015年12月10日
 */
@Controller
public class HuifuNoticeController {
	@Autowired
	private HuifuIntegration huifuIntegration;
		
	private static Logger logger= LoggerFactory.getLogger(HuifuNoticeController.class);
	
	/**
	 * @desc 用户注册后台通知接口示例
	 * @param request
	 * @return
	 */
	@RequestMapping("/notice/huifu/SampleURL")
	public String huifuCallback(ServletRequest request){
		logger.info("[汇付页面转跳接口请求] {}", JSON.toJSONString(request.getParameterMap()));

		// 使用验签函数检查报文签名
		boolean checkResult= true;// huifuIntegration.verifySign(request, HuifuConstant.VERIFY_KEYS_TRANSFER);
		
		if(checkResult){
			// TODO 完成业务逻辑
		
		}
		
		// 通知汇付收到报文 根据接口不同，返回的字段不同
		String returnId= "";
		Map pMap= request.getParameterMap();
		if(pMap.containsKey("OrdId"))
			returnId= request.getParameter("OrdId");
		if(pMap.containsKey("TrxId"))
			returnId= request.getParameter("TrxId");
		if(pMap.containsKey("ProId"))
			returnId= request.getParameter("ProId");
		
		request.setAttribute("returnValue", returnId);	
		return "huifu_notice";
	}

	
}
